package edu.ynu.se.xiecheng.achitectureclass.project.service;

import edu.ynu.se.xiecheng.achitectureclass.common.service.LogicService;
import edu.ynu.se.xiecheng.achitectureclass.project.dao.SoilNitrogenDataDAO;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.Device;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.Greenhouse;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.SoilNitrogenData;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.SoilPHData;
import edu.ynu.se.xiecheng.achitectureclass.project.utils.GreenhouseDeviceUtils;
import edu.ynu.se.xiecheng.achitectureclass.project.utils.SensorApiClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SoilNitrogenDataService extends LogicService<SoilNitrogenDataDAO, SoilNitrogenData, Long> {

    @Autowired
    private SensorApiClient sensorApiClient;

    @Autowired
    private GreenhouseDeviceUtils greenhouseDeviceUtils;

    public SoilNitrogenDataService(SoilNitrogenDataDAO dao) {
        super(dao);
    }

    /**
     * 从传感器API获取含氮量数据并存储到数据库
     */
    public void fetchAndSaveNitrogenData() {
        // 示例 Box 编号和传感器 ID 列表
        String boxNo = "300822040454";
        List<String> sensorIds = List.of("327061586065758008");

        // 调用外部接口获取传感器数据
        List<Map<String, Object>> sensorData = sensorApiClient.fetchSensorData(sensorIds, boxNo);

        for (Map<String, Object> data : sensorData) {
            System.out.println("Sensor data: " + data);

            // 创建或更新大棚信息
            String boxId = data.get("boxId").toString(); // 大棚 ID
            Greenhouse greenhouse = greenhouseDeviceUtils.getOrCreateGreenhouse(boxId);

            // 创建或更新设备信息
            String sensorId = data.get("id").toString(); // 传感器设备 ID
            String name = data.get("name").toString();
            Device device = greenhouseDeviceUtils.getOrCreateDevice(sensorId, name, greenhouse);

            //创建含氮量数据实例并设置属性
            SoilNitrogenData nitrogenData = new SoilNitrogenData();
            nitrogenData.setName(name);
            nitrogenData.setValue(Double.valueOf(data.get("value").toString()));
            nitrogenData.setUnit("mg/L");
            nitrogenData.setTimestamp(new Date());
            nitrogenData.setDevice(device);

            dao.save(nitrogenData);

        }
    }

    /**
     * 获取最新的含氮量数据
     * @return
     */
    public SoilNitrogenData getLatestNitrogenData() {
        SoilNitrogenData soilNitrogenData = dao.findFirstByOrderByTimestampDesc();
        if (soilNitrogenData != null){
            return soilNitrogenData;
        }else
            return new SoilNitrogenData();
    }

    /**
     * 评价含氮量数据的状态
     * @param value
     * @return
     */
    public String evaluateStatus(Double value) {
        if (value == null) return "未知";
        if (value >= 10) return "优";
        if (value >= 0) return "良";
        return "差";
    }



}
